<?php

// +----------------------------------------------
// | Name: 星空框架  图文内容管理
// +----------------------------------------------
// | Version: V5.0
// +----------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +----------------------------------------------
// | Time: 2021-05-29 09:26
// +----------------------------------------------
declare(strict_types=1);

namespace app\xingkong\controller\v1;

use app\common\common\BackendController;
use app\common\model\Artice as ArticeModel;
use app\common\model\Cate as CateModel;
use think\facade\View;
use think\facade\Db;

/**
 * @xk 图文内容管理
 */
class Artice extends BackendController
{
    /**
     * @xk 图文列表
     *
     * @method artice
     *
     * @param ArticeModel $articeModel [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-29T09:28:04+080
     */
    public function artice(ArticeModel $articeModel)
    {
        $param = request()->param();
        $cate_id = isset($param['cate_id']) ? $param['cate_id'] : 0;
        if (request()->isAjax()) {
            $page = isset($param['page']) ? (int) $param['page'] : 1;
            $limit = isset($param['limit']) ? (int) $param['limit'] : 30;
            $keywords = isset($param['keywords']) ? $param['keywords'] : '';
            $condition = [];
            if ('' != $keywords) {
                $condition[] = ['a.title|a.keywords|a.content', 'like', '%'.$keywords.'%'];
            }
            if ($cate_id > 0) {
                $condition[] = ['a.cate_id', '=', $cate_id];
            }
            $list = $articeModel
              ->alias('a')
              ->leftJoin('cate c', 'c.cate_id=a.cate_id')
              ->where($condition)
              ->field('a.`artice_id`,a.`title`,a.`fileurl`,a.`picurl`,a.`order_id`,a.`status`,a.`update_time`,c.`cate_name`,a.`is_tj`')
              ->withattr('picurl',function($value,$data){
                if($value){
                  return getShowUrl($value);
                }
              })
              ->order('order_id desc')
              ->select()
              ->toArray();
            $result_count = $articeModel
                      ->alias('a')
                      ->where($condition)
                      ->count();
            $code = 200;
            $msg = '请求成功';
            $result_code = 200;
            $result_msg = '加载成功';
            $resultData = [];
            $resultData['list'] = $list;
            $this->result($code, $msg, $result_code, $result_msg, $resultData);
        }

        return View::fetch('',['cate_id'=>$cate_id]);
    }

    /**
     * @xk 图文添加
     *
     * @method create
     *
     * @param CateModel $cateModel [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-29T09:39:21+080
     */
    public function create(CateModel $cateModel)
    {
        $param = request()->param();
        $cate_id = isset($param['cate_id']) ? $param['cate_id'] : 0;
        $cate_list = $cateModel->getTreeList('cate_id');

        return View::fetch('', ['cate_id' => $cate_id, 'cate_list' => $cate_list]);
    }

    /**
     * @xk 图文编辑
     *
     * @method edit
     *
     * @param AritceModel $articeModel [description]
     * @param CateModel   $cateModel   [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-29T09:41:24+080
     */
    public function edit(ArticeModel $articeModel, CateModel $cateModel)
    {
        $param = request()->param();
        $artice_id = isset($param['artice_id']) ? $param['artice_id'] : 0;
        if ($artice_id <= 0) {
            $this->error('请选择需要编辑的图文');
        }
        $cate_list = $cateModel->getTreeList('cate_id');
        $info = $articeModel
                ->field('*,picurl as show_picurl,fileurl as show_fileurl')
                ->withattr('show_picurl',function($value,$data){
                  if($value){
                    return getShowUrl($value);
                  }
                })
                ->withattr('show_fileurl',function($value,$data){
                  if($value){
                    return getShowUrl($value);
                  }
                })
                ->find($artice_id);

        return View::fetch('', ['info' => $info, 'cate_list' => $cate_list]);
    }

    /**
     * @xk 图文保存
     *
     * @method save
     *
     * @param ArticeModel $articeModel [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-29T09:44:02+080
     */
    public function save(ArticeModel $articeModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        $resultData = [];
        try {
            $param = request()->param();
            $act = isset($param['act']) ? $param['act'] : 'create';
            $articeModel->validate($param, $act);
            switch ($act) {
            case 'create':
              $res = $articeModel->create($param);
              break;

            case 'edit':
              $res = $articeModel->find($param['artice_id'])->save($param);
              break;
          }
          Db::commit();
            $result_code = 200;
            $result_msg = '操作成功';
        } catch (\Exception $e) {
          Db::rollback();
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }

    /**
     * @xk 图文内容动态保存
     *
     * @method chanageValue
     *
     * @param AritceModel $articeModel [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-29T09:48:46+080
     */
    public function chanageValue(ArticeModel $articeModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $by_key = isset($param['by_key']) ? $param['by_key'] : '';
            $i = 0;
            $j = 0;
            if (!$by_key) {
                throw new \think\Exception("请选择需要修改的数据", 404);

            }
            $by_key_arr = explode(',', $by_key);
            foreach ($by_key_arr as $key => $value) {
                $newarr = [];
                $newarr['by_key'] = $value;
                $newarr['key'] = $param['key'];
                $newarr['value'] = $param['value'];
                $res = $articeModel->chanageValue($newarr);
                if ($res) {
                    ++$i;
                }
            }
            $result_code = 200;
            $result_msg = '处理成功';
            Db::commit();
        } catch (\Exception $e) {
            $result_code = 404;
            $result_msg = $e->getMessage();
            Db::rollback();
        }

        $this->result($code, $msg, $result_code, $result_msg);
    }

    /**
     * @xk 图文内容删除
     *
     * @method destroy
     *
     * @param ArticeModel $articeModel [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-29T09:49:34+080
     */
    public function destroy(ArticeModel $articeModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $id_str = isset($param['id_str']) ? $param['id_str'] : '';
            $i = 0;
            $j = 0;
            if (!$id_str) {
                throw new \think\Exception('请选择需要删除的数据！', 404);
            }
            $id_arr = explode(',', $id_str);
            foreach ($id_arr as $key => $value) {
                $res = $articeModel->destroy($value);
                ++$i;
            }
            Db::commit();
            $result_code = 200;
            $result_msg = '处理成功';
        } catch (\Exception $e) {
            Db::rollback();
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg);
    }
}
